<template>
  <div class="login">
    <div class="card">
      <p style="font-size:20px">用户登录</p>
      <v-form ref="form" v-model="valid" lazy-validation>
        <v-text-field
          label="请输入用户名或手机号"
          outlined
          height="40px"
          dense
          v-model="form.user_name"
          :rules="phoneRules"
          required
        ></v-text-field>
        <v-text-field
          label="请输入密码"
          outlined
          dense
          v-model="form.password"
          :rules="passwordRules"
          required
        ></v-text-field>
        <div class="yzm_box">
          <v-text-field
            label="请验证右侧图形"
            outlined
            height="40px"
            dense
            v-model="form.code"
            :rules="codeRules"
            required
          ></v-text-field>
          <img
            :src="imgurl"
            alt=""
            srcset=""
            style="margin-left:8px;cursor: pointer;width: 130px;height: 40px;"
            @click="getValidateCode"
          />
        </div>
      </v-form>
      <div class="login_info">
        <v-btn text small color="primary"
          ><router-link to="/user/forgetpass"> 忘记密码?</router-link></v-btn
        >
        <v-btn text small color="primary"
          ><router-link to="/user/register">注册</router-link></v-btn
        >
      </div>
      <v-btn
        rounded
        color="primary"
        dark
        width="200px"
        style="margin-top:15px"
        @click="doLogin"
      >
        登 录
      </v-btn>
    </div>
  </div>
</template>

<script>
import { getValidateCode, doLogin } from "@/api/login";

export default {
  name: "login",
  data() {
    return {
      valid: true,
      imgurl: "",
      form: {
        phone: "",
        password: "",
        code: "",
        validate_id: ""
      },
      phoneRules: [
        v => !!v || "手机号不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ],
      passwordRules: [
        v => !!v || "密码不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ],
      codeRules: [
        v => !!v || "图形验证码不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ]
    };
  },

  computed: {},
  created() {
    this.getValidateCode();
  },
  mounted() {},
  methods: {
    // 获取图形验证
    getValidateCode() {
      getValidateCode()
        .then(res => {
          if (res.code === 200) {
            console.log("返回", res);
            this.imgurl = res.data.pic;
            this.form.validate_id = res.data.key;
          } else {
            this.$message.info(res.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 登录
    doLogin() {
      if (
        this.form.code !== "" &&
        this.form.user_name !== "" &&
        this.form.password !== ""
      ) {
        doLogin(this.form)
          .then(res => {
            if (res.code === 200) {
              console.log("返回", res);
              this.$message({
                type: "success",
                message: "登录成功!  " + res.data.username + "  欢迎您!"
              });
              this.$ls.set("token", res.data.token);
              this.$ls.set("username", res.data.username);
              this.$router.push({
                path: "/tlj"
              });
            } else {
              this.$message({
                type: "info",
                message: res.msg
              });
              this.getValidateCode();
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$message({
          type: "info",
          message: "请检查表单"
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.login::v-deep {
  .v-messages__message {
    font-size: 13px;
    line-height: 15px;
  }
  .card {
    width: 100%;
    height: 100%;
    background-color: #fff;
    border-radius: 8px;
    box-sizing: border-box;
    padding: 12px 20px;
    .login_info {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .yzm_box {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
    }
  }
}
</style>
